home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / irit40s.lha / Irit / prsr_lib / ami_srvr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-30  |  5.4 KB  |  189 lines

  1. /*****************************************************************************
  2. *   Routines to handle I/O of objects through Exec message ports             *
  3. *   Server side.                                                             *
  4. *                                                                            *
  5. * Written by:  Kriton Kyrimis                                                *
  6. *        and   Gershon Elber                        Ver 0.2, December 1993.  *
  7. *****************************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <exec/types.h>
  12. #include <exec/ports.h>
  13. #include <exec/memory.h>
  14. #include <dos/dostags.h>
  15.  
  16. #ifdef __SASC
  17. #include <dos.h>
  18. #include <proto/dos.h>
  19. #include <proto/exec.h>
  20. #endif
  21.  
  22. #ifdef __GNUC__
  23. #include <dos/dos.h>
  24. #include <dos/var.h>
  25. #include <inline/dos.h>
  26. #include <inline/exec.h>
  27. #endif
  28.  
  29. #include "irit_sm.h"
  30. #include "irit_soc.h"
  31.  
  32. #include "amiga.h"
  33.  
  34. static struct MsgPort *port = NULL;
  35. static struct MsgPort *replyport = NULL;
  36. static int Active = FALSE;
  37. static int GlblBinaryIPC = FALSE;
  38.  
  39. /*****************************************************************************
  40. *  Sets up the port. Returns TRUE if successful.                             *
  41. *****************************************************************************/
  42. int SocServerCreateSocket(void)
  43. {
  44.     char *name;
  45.  
  46.     GlblBinaryIPC = getenv("IRIT_BIN_IPC") != NULL;
  47.  
  48.     name = getenv("IRIT_SERVER_PORT");
  49.     if (!name) {
  50.     name = IRIT_SERVER_PORT;
  51.     }
  52.     port = CreateMsgPort();
  53.     if (port) {
  54.     port->mp_Node.ln_Name = name;
  55.     port->mp_Node.ln_Pri = 0;
  56.     AddPort(port);
  57.     replyport = CreateMsgPort();
  58.     if (!replyport) {
  59.         RemPort(port);
  60.         DeleteMsgPort(port);
  61.         port = NULL;
  62.         return FALSE;
  63.     }else{
  64.         return TRUE;
  65.     }
  66.     }else{
  67.     return FALSE;
  68.     }
  69. }
  70.  
  71. /*****************************************************************************
  72. *  Returns TRUE if connection is active.                                     *
  73. *****************************************************************************/
  74. int SocServerActive(void)
  75. {
  76.   return (Active);
  77. }
  78.  
  79. /*****************************************************************************
  80. *  Close the port.                                                           *
  81. *****************************************************************************/
  82. void SocServerCloseSocket(void)
  83. {
  84.     if (port) {
  85.     RemPort(port);
  86.     DeleteMsgPort(port);
  87.     port = NULL;
  88.     if (replyport) {
  89.         DeletePort(replyport);
  90.     }
  91.     }
  92.     Active = FALSE;
  93. }
  94.  
  95. /*****************************************************************************
  96. * Block until the client program starts.                                     *
  97. *****************************************************************************/
  98. void SocServerAcceptConnection(void)
  99. {
  100.     int len = -1;
  101.     char buf[4];
  102.  
  103.     buf[0] = '\0';
  104.     while (len < 0) {
  105.     len = GetVar(CLIENT_VAR, buf, sizeof(buf), GVF_GLOBAL_ONLY);
  106.         if (len < 0) {
  107. #ifdef __SASC
  108.         chkabort();
  109. #endif
  110.         Delay(50L);
  111.         }
  112.     }
  113.     DeleteVar(CLIENT_VAR, GVF_GLOBAL_ONLY);
  114. }
  115.  
  116. /****************************************************************************
  117. * Attempt to write an object to the pipe.                                   *
  118. ****************************************************************************/
  119. void SocServerWriteOneObject(IPObjectStruct *PObj)
  120. {
  121.     char *DisplayProg, *ErrorMsg;
  122.     BPTR f;
  123.     char buf[4];
  124.  
  125.     if (!Active) {
  126.         int HaveDisplay;
  127.  
  128.     SetVar(SERVER_VAR, buf, -1, GVF_GLOBAL_ONLY);
  129.  
  130.         /* Needs to start up the client display - it is not up yet */
  131.         DisplayProg = getenv("IRIT_DISPLAY");
  132.         if (DisplayProg == NULL) {
  133.             DisplayProg = "amidrvs -s-";
  134.         }
  135.     f = Open("*", MODE_OLDFILE);
  136.     HaveDisplay = !SystemTags(DisplayProg, SYS_Input,  NULL,
  137.                            SYS_Output, f,
  138.                            SYS_Asynch, TRUE,
  139.                            TAG_END);
  140.     /* SystemTags will always return success for programs run
  141.        asynchronously, so wait a bit and then check if the synchronization
  142.        environment variable created by the client exists. If not, we
  143.        probably failed to start the program.
  144.     */
  145.     Delay(50L);
  146.     if (GetVar(CLIENT_VAR, buf, sizeof(buf), GVF_GLOBAL_ONLY) < 0) {
  147.         HaveDisplay = FALSE;
  148.     }
  149.     if (!HaveDisplay) {
  150.         fprintf(stderr,
  151.             "Irit: Startup your display device - I am waiting...\n");
  152.     }
  153.     SocServerAcceptConnection();
  154.     fprintf(stderr, "Irit: Connection with client established.\n");
  155.     Active = TRUE;
  156.     }
  157.  
  158.     IritPrsrWriteSocket(TRUE);
  159.     if (GlblBinaryIPC) {
  160.         IritPrsrPutBinObject(NULL, PObj);
  161.     }
  162.     else {
  163.     IritPrsrPutObject(NULL, PObj);
  164.     }
  165.     IritPrsrWriteSocket(FALSE);
  166.  
  167.     if (IritPrsrParseError(&ErrorMsg)) {
  168.         fprintf(stderr, "Socket: %s\n", ErrorMsg);
  169.     }
  170. }
  171.  
  172. /*****************************************************************************
  173. *  Write a single line of line length characters.                            *
  174. *****************************************************************************/
  175. void SocServerWriteLine(char *Line, int LineLen)
  176. {
  177.     struct IritMessage msg;
  178.  
  179.     msg.msg.mn_Node.ln_Type = NT_MESSAGE;
  180.     msg.msg.mn_ReplyPort = replyport;
  181.     for (; LineLen>0; LineLen-=msg.nbytes, Line+=msg.nbytes) {
  182.     msg.nbytes = (LineLen > MSGSIZE) ? MSGSIZE : LineLen;
  183.     memcpy(msg.txt, Line, msg.nbytes);
  184.     msg.msg.mn_Length = sizeof(struct IritMessage) - MSGSIZE + msg.nbytes;
  185.     PutMsg(port, (struct Message *)&msg);
  186.     WaitPort(replyport), (void)GetMsg(replyport);
  187.     }
  188. }
  189.